import { Ref, watch } from "vue";
import { patrolingCruise } from "@/api/remote-control.js";
import { controlDevice } from "../shared";

/*
 * @Author: ReinerLau lk850593913@gmail.com
 * @Date: 2023-01-31 17:25:11
 * @LastEditors: ReinerLau lk850593913@gmail.com
 * @LastEditTime: 2023-01-31 17:36:13
 * @FilePath: \v2.0.0\src\views\patroling\composables\usePantilControl.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
interface Options {
  caritem: Ref<string>;
  pantiltDirection: Ref<string>;
}
export default (options: Options) => {
  const { caritem, pantiltDirection } = options;
  let st = null;

  watch(pantiltDirection, async () => {
    clearTimeout(st);
    setPantiltDirection();
  });

  const pantiltDirectionMap = {
    UP: "08",
    LEFT: "04",
    RIGHT: "02",
    DOWN: "16",
  };

  async function setPantiltDirection() {
    if (!pantiltDirection.value) return;
    const data = {
      code: caritem.value,
      param1: "02",
      param2: pantiltDirectionMap[pantiltDirection.value],
      param3: 0,
      param4: 0,
    };
    await patrolingCruise(data);
    st = setTimeout(setPantiltDirection, 200);
  }

  function stopPantilt() {
    const data = {
      code: caritem.value,
      param1: "02",
      param2: "255",
      param3: 0,
      param4: 0,
    };
    controlDevice(data);
  }

  return {
    stopPantilt,
  };
};
